package org.rslai.tcedit.web;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.AccountExpiredException;
import org.springframework.security.BadCredentialsException;
import org.springframework.security.CredentialsExpiredException;
import org.springframework.security.DisabledException;
import org.springframework.security.LockedException;
import org.springframework.security.ui.AbstractProcessingFilter;

import org.rslai.tcedit.web.core.AbstractMvcController;

/**
 * Login controller。为了得到 ACEGI 的错误返回信息。
 * 
 * @author rslai
 * 
 * @spring.bean name="/login.jspx"
 */
public class LoginController extends AbstractMvcController {

	@Override
	public Map getModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String message = "";
		
		HttpSession session = request.getSession();
		
		Object obj = session.getAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);
		if (obj instanceof BadCredentialsException) {
			message = "用户名或口令错误";
		} else if (obj instanceof DisabledException) {
			message = "用户已被禁用";
		} else if (obj instanceof AccountExpiredException) {
			message = "用户帐户已过期";
		} else if (obj instanceof CredentialsExpiredException) {
			message = "用户密码已过期";
		} else if (obj instanceof LockedException) {
			message = "用户已被锁定";
		} else if (obj != null) {
			message = obj.toString();
		}

		Map map = new HashMap();
		map.put("message", message);
		return map;
	}

	@Override
	public String getView(HttpServletRequest request, HttpServletResponse response) {
		return "/login.htm";
	}

}
